package com.vincent;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LogFilter implements Filter {
//	Log log = LogFactory.getLog(this.getClass());
	Log log = LogFactory.getLog("LogFilter");
	String filterName;

	public LogFilter() {
    }

	public void init(FilterConfig fConfig) throws ServletException {
		filterName = fConfig.getFilterName();
		log.info(filterName);
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		chain.doFilter(request, response);
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		
		long startTime = System.currentTimeMillis();
		String requestURI = req.getRequestURI();
		chain.doFilter(request, response);
		long endTime = System.currentTimeMillis();
		log.info(request.getRemoteAddr() + "存取了" + requestURI +"總時數 " + (endTime-startTime) + " 毫秒");
		
	}

	public void destroy() {
		log.info("關閉 Log");
	}

}
